﻿
using Dapper;
using System;
using System.Linq;
using System.Collections.Generic;
using System.Web.Mvc;
using System.Web.Script.Serialization;
using Webdiyer.WebControls.Mvc;
using Ymt.Repository;
using YmtRpc.Domain;
using YmtRpc.Domain.Enum;


namespace YmtRpc.Web.Admin.Controllers
{
    public class SamplingDataController : Controller
    {
        private const int DefaultPageSize = 20;

        /// <summary>
        /// 采样数据分页查询
        /// http://en.webdiyer.com/mvcpager/demos/javascriptapiajax/
        /// </summary>
        /// <param name="appid"></param>
        /// <param name="beginTime"></param>
        /// <param name="endTime"></param>
        /// <param name="dependencyRole"></param>
        /// <param name="page"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public ActionResult Index(string appid, string beginTime, string endTime, string dependencyRole, [Bind(Prefix = "p")]int? page, [Bind(Prefix = "ps")] int? pageSize)
        {
            DateTime beginDateTime, endDateTime;
            IEnumerable<ServiceDependencySampling> list = new List<ServiceDependencySampling>();

            PagedList<ServiceDependencySampling> pageData = new PagedList<ServiceDependencySampling>(list, 0, DefaultPageSize);
      
            beginDateTime = string.IsNullOrEmpty(beginTime) ? DateTime.Now.AddDays(-1) :DateTime.Parse(beginTime);
            endDateTime = string.IsNullOrEmpty(endTime) ? DateTime.Now : DateTime.Parse(endTime);
            DependencyRelationEnum dependencyRelationEnum = (DependencyRelationEnum)Convert.ToInt32(dependencyRole);

            ServiceDependencySamplingQueryCondition queryCond = new ServiceDependencySamplingQueryCondition()
            {
                DependencyRelation = dependencyRelationEnum,
                AppId = appid,
                StartDateTime = beginDateTime,
                EndDateTime = endDateTime
            };

            page = page ?? 1;
            pageSize = pageSize ?? DefaultPageSize;

            PaginationEntity pageInfo = new PaginationEntity()
            {
                PageIndex = page.Value,
                PageSize = pageSize.Value
            };

            list = RepositoryHub.ServiceDependencySamplingRepository.GetServiceDependencySamplingList(queryCond, pageInfo);

            pageData = list.AsQueryable().ToPagedList(page.Value, pageSize.Value);
            pageData.TotalItemCount = pageInfo.TotalCount;
            pageData.CurrentPageIndex = page.Value;

            ViewBag.BeginTime = beginDateTime;
            ViewBag.EndTime = endDateTime;

            if (Request.IsAjaxRequest())
                return PartialView("_SamplingList", pageData);

            return View(pageData);
        }
    }
}
